package com.dome.controller;

import com.alibaba.fastjson.JSON;
import com.dome.common.enumeration.DomeErrorCode;
import com.dome.common.exception.DomeException;
import com.dome.common.pojo.DomeResponse;
import com.dome.common.pojo.PageResponse;
import com.dome.dao.entity.Tickets;
import com.dome.pojo.Tickets.ApportionReq;
import com.dome.pojo.Tickets.MonthDayTotalResp;
import com.dome.pojo.Tickets.TicketsReq;
import com.dome.pojo.Tickets.TicketsVO;
import com.dome.server.TicketsServer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
@RestController("/tickets")
public class TicketsConttroller {

    @Autowired
    private TicketsServer ticketsServer;

    /**
     * 新增
     * @param req
     * @return
     */
    @PostMapping("/addTickets")
    public DomeResponse<String> addTickets(TicketsReq req){
        try {
            log.info("TicketsConttroller.addTickets request: {}", JSON.toJSONString(req));
            ticketsServer.addTickets(req);
            return DomeResponse.success("SUCCESS");
        }catch (DomeException e){
            return DomeResponse.fail(DomeErrorCode.SYSTEM_ERROR.getCode(),e.getMessage());
        }
    }

    /**
     * 删除
     * @param req
     * @return
     */
    @PostMapping("/deleteTickets")
    public DomeResponse<String> deleteTickets(TicketsReq req){
        try {
            log.info("TicketsConttroller.deleteTickets request: {}", JSON.toJSONString(req));
            ticketsServer.deleteTickets(req);
            return DomeResponse.success("SUCCESS");
        }catch (DomeException e){
            return DomeResponse.fail(DomeErrorCode.SYSTEM_ERROR.getCode(),e.getMessage());
        }
    }

    /**
     * 更新
     * @param req
     * @return
     */
    @PostMapping("/updateTickets")
    public DomeResponse<String> updateTickets(TicketsReq req){
        try {
            log.info("TicketsConttroller.updateTickets request: {}", JSON.toJSONString(req));
            ticketsServer.updateTickets(req);
            return DomeResponse.success("SUCCESS");
        }catch (DomeException e){
            return DomeResponse.fail(DomeErrorCode.SYSTEM_ERROR.getCode(),e.getMessage());
        }
    }

    /**
     * 分页查询
     * @param req
     * @return
     */
    @PostMapping("/queryTickets")
    public PageResponse<TicketsVO> queryTickets(TicketsReq req){
        try {
            log.info("TicketsConttroller.queryTickets request: {}", JSON.toJSONString(req));
            return ticketsServer.queryTickets(req);
        }catch (DomeException e){
            throw new DomeException();
        }
    }

    /**
     * 分派
     * @param req
     * @return
     */
    @PostMapping("/fenpai")
    public DomeResponse<String> fenpai(ApportionReq req){
        try {
            log.info("TicketsConttroller.fenpai request: {}", JSON.toJSONString(req));
            return ticketsServer.fenpai(req);
        }catch (DomeException e){
            throw new DomeException(e.getMessage());
        }
    }

    /**
     * 查询指定月份每天的工单总量、超期率
     * @param month
     * @return
     */
    @PostMapping("/queryMonthDayTotal")
    public DomeResponse<List<MonthDayTotalResp>> queryMonthDayTotal(String month){
        try {
            log.info("TicketsConttroller.queryMonthDayTotal request: {}", month);
            return ticketsServer.queryMonthDayTotal(month);
        }catch (DomeException e){
            throw new DomeException(e.getMessage());
        }
    }

    /**
     * 指定查询每个部门的工单总量、超期率
     * @param month
     * @return
     */
    @PostMapping("/queryMonthDepartmentTotal")
    public DomeResponse<List<MonthDayTotalResp>> queryMonthDepartmentTotal(String month){
        try {
            log.info("TicketsConttroller.queryMonthDepartmentTotal request: {}", month);
            return ticketsServer.queryMonthDepartmentTotal(month);
        }catch (DomeException e){
            throw new DomeException(e.getMessage());
        }
    }

    /**
     * 指定查询每个工单类型的工单总量、超期率
     * @param month
     * @return
     */
    @PostMapping("/queryMonthOrderTypeTotal")
    public DomeResponse<List<MonthDayTotalResp>> queryMonthOrderTypeTotal(String month){
        try {
            log.info("TicketsConttroller.queryMonthOrderTypeTotal request: {}", month);
            return ticketsServer.queryMonthOrderTypeTotal(month);
        }catch (DomeException e){
            throw new DomeException(e.getMessage());
        }
    }

}
